//
//  HomePageViewController.m
//  Fragrance
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomePageViewController.h"

@interface HomePageViewController ()

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
 
    
    self.navigationItem.title = @"首页";
    
 [TAOverlay showOverlayWithLabel:nil Options:TAOverlayOptionOverlaySizeRoundedRect];


    


    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 500)];
    UIImage *image = [UIImage imageNamed:@"beijingtu6"];
    imageV.image = image;
    [self.view addSubview:imageV];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    label1.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:239 / 255.0 blue:180 / 255.0 alpha:1];
    [self.view addSubview:label1];
    
    [label1 release];

    [imageV release];

    
    //提示的同时加载数据
    [self downLoadThisPage];

}

+(void)showOverlayWithLabel:(NSString *)status Options:(TAOverlayOptions)options
{
    
}

- (void)downLoadThisPage
{
    NSString *str = @"http://api.2meiwei.com/v1/index/";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        [TAOverlay hideOverlay];
       

        
        //数据请求完成，开始解析
        self.arrayData = [NSMutableArray array];
        self.arrayData = [HomeFirstJieXi getHomeModel:data];
        
        self.arrayDataNext = [NSMutableArray array];
        self.arrayDataNext = [HomeFirstNextJX getHomeModel:data];
        
        
        
        //数据解析完成，可以加载视图了
        [self inithomeTableView];
        
    }];
    
    
}




- (void)inithomeTableView
{
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 20)];
    label.backgroundColor = [UIColor whiteColor];
    label.text = @"编辑推荐";
    label.font = [UIFont systemFontOfSize:15.0];
    [homeScrollView addSubview:label];
    [label release];

    
    FirstScrollView *scrollView = [[FirstScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    scrollView.dataArr = [NSMutableArray array];
    scrollView.dataArr = self.arrayData;
    
    homeTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStylePlain];
    homeTableview.delegate = self;
    homeTableview.dataSource = self;
    homeTableview.tableHeaderView = scrollView;

    [homeTableview setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    homeTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:homeTableview];
    
    

    [scrollView release];
    [homeTableview release];
}





- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"cell";
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    HomeModel *model = [self.arrayDataNext objectAtIndex:indexPath.row];
    cell.caiName.text = model.title;
    cell.details.text = model.miaoshu;
    [cell.icon setImageWithURL:[NSURL URLWithString:model.tupian] placeholderImage:[UIImage imageNamed:@"jiazaitu"]];
    
    UIImage *image = [UIImage imageNamed:@"beijingtu5"];
    UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
    
    cell.backgroundView = imageview;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arrayDataNext.count;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"编辑推荐";
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HomeModel *model = [self.arrayDataNext objectAtIndex:indexPath.row];
    FirstDetailsViewController *first = [[FirstDetailsViewController alloc] init];
    first.name = model.title;
    first.ID = model.nextId;
    [self.navigationController pushViewController:first animated:YES];
  
    [first release];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
